package com.kk.bird;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

/**
 * @author kzy
 * @version 1.0
 * @date 2023/2/7
 */

public class BirdGame extends JPanel {
    BufferedImage background = ImageIO.read(this.getClass().getResource(GameConfig.pictureUrl+"bg.png"));
    BufferedImage startImage = ImageIO.read(this.getClass().getResource(GameConfig.pictureUrl+"start.png"));
    BufferedImage gameOverImage = ImageIO.read(this.getClass().getResource(GameConfig.pictureUrl+"gameover.png"));
    Ground ground = new Ground();
    Column column1 = new Column(1);
    Column column2 = new Column(2);
    Bird bird = new Bird();
    int score = 0;
    int state = 0;
    public static final int START = 0;
    public static final int RUNNING = 1;
    public static final int GAME_OVER = 2;

    public BirdGame() throws Exception {
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(this.background, 0, 0, (ImageObserver)null);
        g.drawImage(this.ground.image, this.ground.x, this.ground.y, (ImageObserver)null);
        g.drawImage(this.column1.image, this.column1.x - this.column1.width / 2, this.column1.y - this.column1.height / 2, (ImageObserver)null);
        g.drawImage(this.column2.image, this.column2.x - this.column2.width / 2, this.column2.y - this.column2.height / 2, (ImageObserver)null);
        Graphics2D g2 = (Graphics2D)g;
        g2.rotate(-this.bird.alpha, (double)this.bird.x, (double)this.bird.y);
        g.drawImage(this.bird.image, this.bird.x - this.bird.width / 2, this.bird.y - this.bird.height / 2, (ImageObserver)null);
        g2.rotate(this.bird.alpha, (double)this.bird.x, (double)this.bird.y);
        Font f = new Font("SansSerif", 1, 40);
        g.setFont(f);
        g.drawString("" + this.score, 40, 60);
        g.setColor(Color.WHITE);
        g.drawString("" + this.score, 37, 57);
        switch(this.state) {
            case 0:
                g.drawImage(this.startImage, 0, 0, (ImageObserver)null);
                break;
            case 2:
                g.drawImage(this.gameOverImage, 0, 0, (ImageObserver)null);
        }

    }

    public void action() throws Exception {
        MouseListener l = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                try {
                    switch(BirdGame.this.state) {
                        case 0:
                            BirdGame.this.state = 1;
                            break;
                        case 1:
                            BirdGame.this.bird.flappy();
                            break;
                        case 2:
                            BirdGame.this.column1 = new Column(1);
                            BirdGame.this.column2 = new Column(2);
                            BirdGame.this.bird = new Bird();
                            BirdGame.this.score = 0;
                            BirdGame.this.state = 0;
                    }
                } catch (Exception var3) {
                    var3.printStackTrace();
                }

            }
        };
        this.addMouseListener(l);

        while(true) {
            switch(this.state) {
                case 0:
                    this.bird.fly();
                    this.ground.step();
                    break;
                case 1:
                    this.ground.step();
                    this.column1.step();
                    this.column2.step();
                    this.bird.fly();
                    this.bird.step();
                    if (this.bird.x == this.column1.x || this.bird.x == this.column2.x) {
                        ++this.score;
                    }

                    if (this.bird.hit(this.ground) || this.bird.hit(this.column1) || this.bird.hit(this.column2)) {
                        this.state = 2;
                    }
            }

            this.repaint();
            Thread.sleep(16L);
        }
    }

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame();
        BirdGame game = new BirdGame();
        frame.add(game);
        frame.setSize(440, 670);
        frame.setLocationRelativeTo((Component)null);
        frame.setDefaultCloseOperation(3);
        frame.setVisible(true);
        game.action();
    }
}

